package morpion;

import java.io.IOException;
import java.util.Scanner;

import neurones.Reseau;

public class MorpionMain {

	/**
	 * @param args
	 * @throws 

	 */
	public static void main(String[] args) throws IOException {

		Morpion m=new Morpion();
		double[][] dump;
		double[] expected;
		double gainCalc;
		int[] layer = {11, 22, 22, 9}; //input : isCentre, nbCoin, totalPiece //sortie : 1,2,3/4,5,6/7,8,9
		Reseau r1 = new Reseau(layer);
		//Reseau r2 = new Reseau(layer);
		int lose = 0;
		boolean plop = false;

		r1.randomize();

		Player c1=new Computer(Morpion.WHITE, null);
		Player c2=new Computer(Morpion.BLACK, r1);

		//Player h1=new Human(Morpion.WHITE,"Mathieu");

		while(true){
			System.out.println("Voulez faire jouer IAvsIA ou CoopvsIA (1/2)");
			Scanner sc=new Scanner(System.in);
			int choix=sc.nextInt();
			m = new Morpion();
			c1.coups.clear();
			c2.coups.clear();

			if(choix==1)
			{
				//Player h1=new Human(Morpion.WHITE,"Mathieu");


				System.out.println(m.toString());

				//JEU
				for(int i = 0; i < 2500000; i++)
				{
					m = new Morpion();
					c1.coups.clear();
					c2.coups.clear();
					c1.nb_partie++;
					c2.nb_partie++;
					
					Player tmp = c1;
					c1 = c2;
					c2 = tmp;
					
				

					while(true){
						if(m.end()==2){
							c1.jouer(m);
							if (i%100001 == 0) System.out.println(m.toString());
						}
						else break;
						if(m.end()==2){
							c2.jouer(m);
							if (i%100001== 0) System.out.println(m.toString());
						}
						else break;
					}

					plop = false;

					for(int j = 0; j < Morpion.ROW; j++)
					{
						for(int k = 0; k < Morpion.COL; k++)
						{
							if (m.get(j, k) == Morpion.EMPTY) plop = true;
						}
					}
					if (!plop) lose++;


					if (m.end() == Morpion.WHITE)
					{
						if (c1.color == Morpion.WHITE) c1.nb_vict++;
						else c2.nb_vict++;

						if (c1.r != null) for(int j = 0; j < c1.coups.size(); j++)
						{
							dump = c1.coups.get(j).getDumpReseau();
							gainCalc = c1.coups.get(j).getEstimationScore();
							expected = new double[Morpion.COL * Morpion.ROW];

							expected[c1.coups.get(j).getCoup()] = Coup.calcGain(1-1./((c1.coups.size()%3)+1), gainCalc, j);
							c1.r.learn(expected);					
						}
						if (c2.r != null) for(int j = 0; j < c2.coups.size(); j++)
						{
							dump = c2.coups.get(j).getDumpReseau();
							gainCalc = c2.coups.get(j).getEstimationScore();
							expected = new double[Morpion.COL * Morpion.ROW];

							expected[c2.coups.get(j).getCoup()] = Coup.calcGain(1-1./((c1.coups.size()%3)+1), gainCalc, j);
							if (c2.r != null)c2.r.setValues(dump);
							c2.r.learn(expected);
						}
					}
					else if (m.end() == Morpion.BLACK)
					{
						if (c1.color == Morpion.BLACK) c1.nb_vict++;
						else c2.nb_vict++;
						
						if (c1.r != null) for(int j = 0; j < c1.coups.size(); j++)
						{
							dump = c1.coups.get(j).getDumpReseau();
							gainCalc = c1.coups.get(j).getEstimationScore();
							expected = new double[Morpion.COL * Morpion.ROW];
							expected[c1.coups.get(j).getCoup()] = Coup.calcGain(1./((c1.coups.size()%3)+1), gainCalc, j);
							if (c1.r != null) c1.r.setValues(dump);
							if (c1.r != null) c1.r.learn(expected);					
						}
						if (c2.r != null) for(int j = 0; j < c2.coups.size(); j++)
						{
							dump = c2.coups.get(j).getDumpReseau();
							gainCalc = c2.coups.get(j).getEstimationScore();
							expected = new double[Morpion.COL * Morpion.ROW];

							expected[c2.coups.get(j).getCoup()] = Coup.calcGain(1./((c1.coups.size()%3)+1), gainCalc, j);
							c2.r.setValues(dump);
							c2.r.learn(expected);
						}
					}
					else
					{
						c1.nb_nul++;
						c2.nb_nul++;

						if (c1.r != null) for(int j = 0; j < c1.coups.size(); j++)
						{
							dump = c1.coups.get(j).getDumpReseau();
							gainCalc = c1.coups.get(j).getEstimationScore();
							expected = new double[Morpion.COL * Morpion.ROW];

							expected[c1.coups.get(j).getCoup()] = Coup.calcGain(0.3, gainCalc, j);
							c1.r.setValues(dump);
							c1.r.learn(expected);					
						}
						if (c2.r != null) for(int j = 0; j < c2.coups.size(); j++)
						{
							dump = c2.coups.get(j).getDumpReseau();
							gainCalc = c2.coups.get(j).getEstimationScore();
							expected = new double[Morpion.COL * Morpion.ROW];

							expected[c2.coups.get(j).getCoup()] = Coup.calcGain(0.3, gainCalc, j);
							c2.r.setValues(dump);
							c2.r.learn(expected);
						}
					}
				}

				System.out.println(c1.nb_partie+":"+c1.nb_vict+":"+c1.nb_nul);
				System.out.println(c2.nb_partie+":"+c2.nb_vict+":"+c2.nb_nul);
				System.out.println((lose-c2.nb_nul));

			}

			else
			{
				System.out.println("Voulez vous jouer en premier(1) ou en deuxieme(2) ?");
				Scanner s=new Scanner(System.in);
				int c=s.nextInt();
				if(c==1)
				{
					Player c3=new Human(Morpion.WHITE, null);
					Player c4=new Computer(Morpion.BLACK, r1);
					System.out.println(m.toString());

					while(m.end()==2){
						c3.jouer(m);
						System.out.println(m.toString());
						if(m.end()==2){
							c4.jouer(m);
							System.out.println(m.toString());
						}
					}
					int f=m.end();
					if(f==1)
					{
						System.out.println("Vous avez gagne!! =)");
					}
					else if(f==0)
					{
						System.out.println("Vous avez perdu!! =(");
					}
					else
					{
						System.out.println("Egalite");
					}

				}
				else
				{
					Player c4=new Computer(Morpion.BLACK, r1);
					Player c3=new Human(Morpion.WHITE, r1);
					System.out.println(m.toString());

					while(m.end()==2){
						c4.jouer(m);
						System.out.println(m.toString());
						if(m.end()==2){
							c3.jouer(m);
							System.out.println(m.toString());
						}
					}
					int f=m.end();
					if(f==0)
					{
						System.out.println("Vous avez gagne!! =)");
					}
					else if(f==1)
					{
						System.out.println("Vous avez perdu!! =(");
					}
					else
					{
						System.out.println("Egalite");
					}
				}
			}


		}
	}
}
